var express = require('express')
var app = express()
var path = require('path')
// 引入border-paaser
var bodyParser = require('body-parser')
// // 注册
// app.post('/test', function (req, res) {
//   res.header('Access-Control-Allow-Origin', '*')
//   res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
//   res.header('Access-Control-Allow-Headers', 'X-Requested-With')
//   res.header('Access-Control-Allow-Headers', 'Content-Type')
//   // res.send('This is a test service !')
//   con.query('SELECT * from `user-list`', function (err, data) {
//     res.end(JSON.stringify(data))
//   })
// })
app.all('*', function (req, res, next) {
  // console.log(req.method);
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'Content-type')
  res.header(
    'Access-Control-Allow-Methods',
    'PUT,POST,GET,DELETE,OPTIONS,PATCH',
  )
  res.header('Access-Control-Max-Age', 1728000) //预请求缓存20天
  next()
})
// app.get('/img', function (req, res) {
//   res.header('Access-Control-Allow-Origin', '*')
//   res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
//   res.header('Access-Control-Allow-Headers', 'X-Requested-With')
//   res.header('Access-Control-Allow-Headers', 'Content-Type')
//   res.sendFile(path.join(__dirname, 'public/uploads/upload-1641543755115.jpg'))
// })
var account = require('./Routes/account')
var admin = require('./Routes/admin')
app.use('', account)

app.use('', admin)
app.use(express.static('public'))
app.get('/public/images/', function (req, res) {
  res.sendFile(__dirname + '/' + req.url)
  console.log('Request for ' + req.url + ' received.')
})
app.get('/public/js/', function (req, res) {
  res.sendFile(__dirname + '/' + req.url)
  console.log('Request for ' + req.url + ' received.')
})
app.get('/public/css/', function (req, res) {
  res.sendFile(__dirname + '/' + req.url)
  console.log('Request for ' + req.url + ' received.')
})
app.get('/test', function (req, res) {
 res.send('This is a test server!')
})
app.listen(8080, () => {
  console.log('server start')
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.urlencoded({ extended: true }))
